package cn.thinkluck.pattern.builder;

import lombok.Setter;
import lombok.ToString;

/**
 * 创建者模式示例
 * 能够以各种形式来构建同一个对象
 * @Author liutao
 * @DateTime 2024/7/19 下午10:08
 */
@Setter
@ToString
public class HtmlDocument {
    //模拟文档对象
    private String header = "";
    private String body = "";
    private String footer = "";

    /**
     * 测试创建
     * @param args
     */
    public static void main(String[] args) {
        HtmlDocument document = new HtmlDocument.Builder()
                .setHeader("header")
                .setBody("body")
                .setFooter("footer")
                .build();
        System.out.println(document);
    }

    /**
     * 创建者类
     * 复杂的创建者可以抽象为接口，使用不同的创建者构建对象
     */
    public static class Builder {
        protected HtmlDocument document;

        public Builder() {
            document = new HtmlDocument();
        }

        public Builder setHeader(String header) {
            document.setHeader(header);
            return this;
        }

        public Builder setBody(String body) {
            document.setBody(body);
            return this;
        }

        public Builder setFooter(String footer) {
            document.setFooter(footer);
            return this;
        }

        public HtmlDocument build() {
            return document;
        }
    }
}
